package com.common.baselibrary.progress;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.common.baselibrary.progressutil.KProgressHUD;

/**
 * Dialog的进度控制
 */

public class ProgressDialogHandler extends Handler {
    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int DISMISS_PROGRESS_DIALOG = 2;

    private KProgressHUD hud;
    private Context context;

    public ProgressDialogHandler(Context context) {
        super();
        this.context = context;
    }

    private void initProgressDialog() {

        if (hud == null) {
            hud = KProgressHUD.create(context);
        }

        if (!hud.isShowing()) {
            hud.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setDimAmount(0.5f)
                    .setLabel("正在加载...")
                    .show();
        }

    }

    private void dismissProgressDialog() {
        if (hud != null) {
            hud.dismiss();
            hud = null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog();
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }
}
